package cn.edu.ecut.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;

public class SecondServer {

	public static void main(String[] args) throws IOException {
		
		ServerSocket server = new ServerSocket();
		
		SocketAddress socketAddress = new InetSocketAddress( "1.0.0.99" , 5555 );
		server.bind( socketAddress );
		
		while( true ) {
			System.out.println( "正在监听..." );
			Socket s = server.accept();
			System.out.println( "监听到: " + s );
			
			InputStream in = s.getInputStream(); // 获得可以从客户端读取数据的字节输入流
			InputStreamReader rd = new InputStreamReader( in );
			BufferedReader br = new BufferedReader( rd );
			
			
			String message ;
			
			while( ( message = br.readLine() ) != null ) {
				System.out.println( "接收到来自客户端的数据: " + message );
				if( "byebye".equalsIgnoreCase( message ) ) {
					break ;
				}
			}
			
			br.close();
			rd.close();
			in.close();
		}
		
		// server.close(); // Unreachable code
		
	}

}
